package nowcode;

public class DeleteDuplicationSolution {


    public static void main(String[] args) {

    }

    public ListNode deleteDuplication(ListNode pHead) {

        ListNode prev = new ListNode(0);

        ListNode head = prev;


        while (pHead.next != null) {
            if (pHead.val != pHead.next.val) {
                prev.next = pHead;
                prev = pHead;
                pHead = pHead.next;
                continue;
            }
            while (pHead.next != null && pHead.val == pHead.next.val) {
                pHead = pHead.next;
            }
            pHead = pHead.next;
            if (pHead == null) {
                prev.next = null;
                break;
            }
            if (pHead.next == null) {
                prev.next = pHead;
                break;
            }
        }

        return head.next;
    }
}

class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
